const api = require('../util')
const app = getApp()
Component({
    data: {
        select: 0,
        is_bind_phone: '',
        list: [
            {
                pagePath: "/pages/home/home",
                text: "首页",
                iconPath: "/images/home2.png",
                selectedIconPath: "/images/home1.png",
                type: 0
              },
              {
                pagePath: "/pages/clockin/clockin",
                text: "打卡",
                iconPath: "/images/clockin1.png",
                selectedIconPath: "/images/clockin2.png",
                type: 0
              },
              {
                pagePath: "/pages/gangcircle/gangcircle",
                text: "帮帮圈",
                iconPath: "/images/gangcircle1.png",
                selectedIconPath: "/images/gangcircle2.png",
                type: 0
              },
              {
                pagePath: "/pages/my/my",
                text: "我的",
                iconPath: "/images/my1.png",
                selectedIconPath: "/images/my2.png",
                type: 0
              }
        ]
    },
    lifetimes: {
      // 在组件实例刚刚被创建时执行
      created: function () {
        var that = this
        var timeis_bind_phone = ''
        if (this.data.is_bind_phone == '') {
          timeis_bind_phone = setInterval(() => {
            if (wx.getStorageSync(app.globalData.miniProgramtype+'is_bind_phone') === 1) {
              clearInterval(timeis_bind_phone)
              that.setData({
                is_bind_phone: wx.getStorageSync(app.globalData.miniProgramtype+'is_bind_phone')
              })
            } else if (wx.getStorageSync(app.globalData.miniProgramtype+'is_bind_phone') === 0) {
              that.setData({
                is_bind_phone: 0
              })
            }
          }, 500);
        }
      },
      // 在组件实例进入页面节点树时执行
      attached: function () {},
      // 在组件实例被从页面节点树移除时执行
      detached: function () { },
    },
    methods: {
        selectPage(e) {
            const { index, page, type } = e.currentTarget.dataset;
            if (index !== this.data.select && type === 0) {
                if (page == '/pages/clockin/clockin') {
                  if(!api.disable()) {
                    return
                }
                  var that = this
                  // 判断是否设置用药提醒
                  var that = this
                  if (wx.getStorageSync(app.globalData.miniProgramtype+'access_token')=='') {
                      app.wxlogin().then(()=>{
                        api.http('/MedicationReminder/normalQuantity', 'POST', false, {
                        }, function (res) {
                          if(res.data.count !== 0) {
                            wx.switchTab({
                              url: page,
                            })
                          } else {
                            wx.showModal({
                              title: '提示',
                              content: '未添加用药提醒，是否前往添加？',
                              success: function (sm) {
                                if (sm.confirm) {
                                  wx.navigateTo({
                                    url: '/pages/remind/remind',
                                  })
                                } else if (sm.cancel) {}
                              }
                          })
                          }
                        })
                        return
                      })
                  } else {
                    api.http('/MedicationReminder/normalQuantity', 'POST', false, {
                    }, function (res) {
                      if(res.data.count !== 0) {
                        wx.switchTab({
                          url: page,
                        })
                      } else {
                        wx.showModal({
                          title: '提示',
                          content: '未添加用药提醒，是否前往添加？',
                          success: function (sm) {
                            if (sm.confirm) {
                              wx.navigateTo({
                                url: '/pages/remind/remind',
                              })
                            } else if (sm.cancel) {}
                          }
                      })
                      }
                    })
                    return
                  }
                }
                wx.switchTab({
                  url: page,
                })
            }
        },
        getPhoneNumber (e) {
          var that = this
          if (e.detail.errMsg != 'getPhoneNumber:fail no permission'&&e.detail.errMsg != 'getPhoneNumber:fail user deny') {
            console.log(e)
            api.http('/login/bindPhone', 'POST', false, {
                code: e.detail.code
            }, function (res) {
              if(res.code == 200) {
                 that.setData({
                    is_bind_phone: 1
                })
                wx.setStorageSync(app.globalData.miniProgramtype+'is_bind_phone',1)
                const pages = getCurrentPages()
                const perpage = pages[pages.length - 1]
                perpage.onLoad()
                perpage.onShow()
                wx.showToast({
                  title: res.msg,
                  icon: 'success',
                  duration: 1000
                })
              } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'none',
                    duration: 1000
                })
              }
            })
          }
      },
    }
})